package org.example;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import org.example.handler.ChatSocketClientHandler;
import org.example.handler.NettyClientHandler;

import java.net.InetSocketAddress;

/**
 * Hello world!
 */
public class NettyClient {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventExecutors)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel nc) throws Exception {
                        nc.pipeline().addLast(new LineBasedFrameDecoder(2048));
                        nc.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
                        nc.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
                      //  nc.pipeline().addLast(new NettyClientHandler());
                        nc.pipeline().addLast(new ChatSocketClientHandler());
                    }
                }).connect(new InetSocketAddress("localhost", 9998))
                .channel()
                .closeFuture()
                .sync();


    }
}
